package ipark.sistemski;

/* KLAS KI BO ZACASNO SHRANJEVAL ERRORE V VEKOTR IN TUDI TE OPRAVLJAL
 * IZ BAZE BO PREBRAL LASTNOSTI NAPAK IN JIH VPISAL V KLASS NAPAKA.JAVA
 * POTEM PA BO TO SHRANIL V VEKTOR */
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ShraniZacasno {
 Vector vek = new Vector();
     
     public Vector getFromDB(){ 
/*       String sql = "SELECT * FROM errors";
        try {
            ResultSet data = DBConnect.statement().executeQuery(sql);
            while(data.next()){
                napaka np = new napaka();
                np.setStatus(data.getBoolean("type"));
                np.setDatumNap(data.getDate("dat"));
                np.setOpisNap(data.getString("description"));
                np.setOddelek(data.getInt("place"));
                vek.addElement(np);
            }
           } catch (SQLException ex) {
               System.out.println("<<Napaka pri SQL stavku>>");
            Logger.getLogger(ShraniZacasno.class.getName()).log(Level.SEVERE, null, ex);
        }*/
         return vek; 
     }

     public void  setToDB(napaka tmp){
        try {
            PreparedStatement ps2;
            String sql2 = "INSERT INTO errors (type,dat,description,place) VALUES (?,?,?,?)";
            ps2 = DBConnect.connection().prepareStatement(sql2);
            ps2.setBoolean(1, tmp.isFixed());
            //kak castas util.Date -> sql.Date
            Calendar stCal = Calendar.getInstance();
            java.util.Date utlDate = stCal.getTime();
            java.sql.Date sqlDate = new java.sql.Date(utlDate.getTime());
            ps2.setDate(2, sqlDate);
            ps2.setBytes(3, tmp.getOpisNap().getBytes("UTF8"));
            ps2.setInt(4, tmp.getOddelek());
            ps2.execute();
            vek.addElement(tmp);
            DBConnect.close();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(ShraniZacasno.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ShraniZacasno.class.getName()).log(Level.SEVERE, null, ex);
        }
     }
    
}